package com.situ.mall.service.Impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.situ.common.util.PageResult;
import com.situ.mall.mapper.AdminMapper;
import com.situ.mall.pojo.entity.Admin;
import com.situ.mall.pojo.query.AdminQuery;
import com.situ.mall.service.IAdminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
@Service
public class AdminServiceImpl implements IAdminService {
    @Autowired
    private AdminMapper adminMapper;

    @Override
    public PageResult list(AdminQuery adminQuery) {

        //使用PageHelper开启分页
        PageHelper.startPage(adminQuery.getPage(), adminQuery.getLimit());
        //开启分页之后 LIMIT offset,pageSize 这个代码PageHelper帮我们做了
        List<Admin> list = adminMapper.list(adminQuery);
        //查找总数量PageHelper会自动帮我们完成
        //int totalCount = userMapper.selectTotalCount(userQuery);
        PageInfo pageInfo = new PageInfo(list);
        int totalCount = (int) pageInfo.getTotal();

        return PageResult.ok(totalCount, list);
    }

    @Override
    public Admin login(String account, String password) {
        return adminMapper.login(account, password);
    }

    @Override
    public void update(Admin admin) {
        adminMapper.updateByPrimaryKeySelective(admin);
    }

    @Override
    public Integer selectByAccount(Admin admin) {
        return adminMapper.selectByAccount(admin);
    }

    @Override
    public void add(Admin admin) {
        adminMapper.insertSelective(admin);
    }

    @Override
    public Admin selectById(Integer id) {
        return adminMapper.selectByPrimaryKey(id);
    }

    @Override
    public Integer selectByAccountId(Admin admin) {
        return adminMapper.selectByAccountId(admin);
    }

    @Override
    public void deleteAll(Integer[] ids) {
        adminMapper.deleteAll(ids);
    }

    @Override
    public void deleteById(Integer id) {
        adminMapper.deleteByPrimaryKey(id);
    }

    @Override
    public void updatePassword(Integer id, String newPassword) {
        Admin admin = new Admin();
        admin.setId(id);
        admin.setPassword(newPassword);
        adminMapper.updateByPrimaryKeySelective(admin);
    }

}
